package com.order.service.impl;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.order.mapper.SellerInfoMapper;
import com.order.mapper.UserMapper;
import com.order.pojo.SellerInfo;
import com.order.pojo.User;
import com.order.service.ISellerInfoService;
import com.order.service.UserService;
import com.order.utils.Result;
import com.order.utils.TokenUtil;
import com.order.vo.PassWordVO;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * 用户服务实现类
 * 实现了用户相关的业务逻辑
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    // 注入UserMapper
    @Autowired
    private UserMapper userMapper;

    @Qualifier("jasyptStringEncryptor")
    @Autowired
    private StringEncryptor encryptor;

    /**
     * 根据用户名查询用户
     */
    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    /**
     * 用户登录业务逻辑
     */
    @Override
    public User login(String username, String password) {
        // 1. 根据用户名查询用户
        User user = userMapper.findByUsername(username);

        // 2. 验证用户是否存在
        if (user == null) {
            return null; // 用户不存在
        }

        // 3. 验证密码是否正确
        // 使用Jasypt加密器对密码进行解密比较
        // 注意：这里假设user.getPassword()和password都是加密后的字符串
        // 如果密码是明文存储的，则直接比较即可
        if (encryptor.decrypt(user.getPassword()).equals(password)) {
            // 密码正确，返回用户对象
            return user;
        } else {
            // 密码错误
            return null;
        }
    }

    /**
     * 用户注册业务逻辑
     */
    @Override
    public boolean register(User user) {
        // 1. 检查用户名是否已存在
        User existingUser = userMapper.findByUsername(user.getUsername());
        if (existingUser != null) {
            return false; // 用户名已存在，注册失败
        }

        // 2. 对密码进行MD5加密后再存储


        // 3. 插入新用户
        int rows = userMapper.insert(user);
        // 返回插入结果，1表示成功
        return rows == 1;
    }

    @Override
    public Result updatePwd(PassWordVO passWordVO) {
        String oldPassword = passWordVO.getOldPassword();
        String newPassword = passWordVO.getNewPassword();
        String confirmPassword = passWordVO.getConfirmPassword();
        String username = passWordVO.getUsername();
        if(oldPassword == null || oldPassword.isEmpty() || newPassword == null || newPassword.isEmpty() || confirmPassword == null || confirmPassword.isEmpty()){
            return Result.error("密码不能为空");
        }
        if(!newPassword.equals(confirmPassword)){
            return Result.error("两次输入的新密码不一致");
        }
        User user = userMapper.findByUsername(username);
        if(user==null){
            return Result.error("用户不存在");
        }
        if (!encryptor.decrypt(user.getPassword()).equals(oldPassword)) {
            return Result.error("旧密码错误");
        }
        if(oldPassword.equals(newPassword)){
            return Result.error("新密码不能与旧密码相同");
        }
        // 对新密码进行加密
        String encryptedNewPassword = encryptor.encrypt(newPassword);
        // 更新用户密码
        user.setPassword(encryptedNewPassword);
        user.setUpdateTime(DateUtil.now());
        int rows = userMapper.updateById(user);
        if (rows > 0) {
            return Result.success("密码更新成功");
        }
        return Result.error("更新失败");
    }
}
    